function _ajax({ url, method = 'GET', data }) {
  return new Promise((resolve, reject) => {
    let xhr = new XMLHttpRequest();

    if (method == 'GET') {
      // get请求
      url = `${url}?`; // get请求参数需要拼接？
      // 在？后面拼接请求参数
      for (let key in data) {
        url += `${key}=${data[key]}&`;
      }
      url.slice(0, -1); // 切割最后的&

      xhr.open(method, url);
      xhr.send(null);
    } else if (method == 'POST') {
      // post
      let a = '';
      for (var keys in data) {
        a += `${keys}=${data[keys]}&`;
      }
      xhr.open(method, url);
      xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); // 设置请求头，将数据以key/value 的形式传输到服务器
      xhr.send(a);
    }

    xhr.addEventListener('readystatechange', () => {
      if (xhr.readyState == 4) {
        if (xhr.status == 200) {
          let res = xhr.responseText;
          resolve(res);
        } else {
          let res = xhr.responseText;
          reject(res);
        }
      }
    });
  });
}


// 点击下载图片
function downloadDirect(url) {
  const aTag = document.createElement('a');
  aTag.download = 'qrcode';
  aTag.href = url;
  aTag.click();
}
